<%@ page import="com.the8thfold.anton.users.User; com.the8thfold.anton.ant.AntScriptUser; org.codehaus.groovy.grails.plugins.springsecurity.SpringSecurityUtils; com.the8thfold.anton.users.Roles; com.the8thfold.anton.ant.JobStatus" contentType="text/html;charset=UTF-8" %>
<html>
<head>
    <title>Jobs</title>
    <meta name="layout" content="main" />
</head>
<body>

<div>
    <g:if test="${script}">
        <h1>Jobs of '${script.description}'</h1>
    </g:if>
    <g:else>
        <h1>Jobs</h1>
    </g:else>
    <hr/>
    <br/>
    <div class="css_right toolbar">
        <g:if test="${SpringSecurityUtils.ifAllGranted(Roles.ROLE_JOB_ADMIN.toString()) || AntScriptUser.countByUser(User.findByUserId(sec.loggedInUserInfo(field: 'userId'))) > 0}">
            <button class="btn-text" ${SpringSecurityUtils.ifAllGranted(Roles.ROLE_JOB_ADMIN.toString()) ?: 'disabled'} onclick="document.location.href='${createLink(controller: 'job', action: 'create', params: [scriptId: script?.scriptId])}'">Create Job</button>
        </g:if>
    </div>
    <br/>
    <br/>
    <br/>
    <table>
        <tr>
            <th>Name</th>
            <th>Description</th>
            <th>Script</th>
            <th>Status</th>
            <th>Created By</th>
            <th>Run Type</th>
            <th># Runs</th>
            <th></th>
        </tr>
        <g:each in="${jobs}" status="jobIndex" var="job">
            <tr class="${jobIndex%2 ? 'odd' : 'even'}">
                <td>
                    <g:link controller="job" action="view" id="${job.jobId}">${job.name}</g:link>
                </td>
                <td>${job.description}</td>
                <td>
                    <g:if test="${SpringSecurityUtils.ifAllGranted(Roles.ROLE_SCRIPT_ADMIN.toString())}">
                        <g:link controller="script" action="view" id="${job.script.scriptId}">${job.script.description}</g:link>
                    </g:if>
                    <g:else>${job.script.description}</g:else>
                </td>
                <td>${job.jobStatus.toString()}</td>
                <td title="${job.dateCreated}">${job.createdBy.firstName + " " + job.createdBy.lastName} (<prettytime:display date="${job.dateCreated}" />)</td>
                <td>
                    <g:if test="${job.schedule}">
                        ${job.schedule}
                    </g:if>
                    <g:else>
                        Manual
                    </g:else>
                </td>
                <td>
                    <g:link controller="result" action="list" params="[jobId: job.jobId]">${job.runResults?.size()}</g:link>
                </td>
                <td>
                    <div class="toolbar">
                        <g:if test="${job.isActive()}">
                            <button class="" ${SpringSecurityUtils.ifAllGranted(Roles.ROLE_JOB_ADMIN.toString()) ?: 'disabled'} onclick="document.location.href='${createLink(controller: 'job', action: 'pause', id: job.jobId, params: [origin: request.forwardURI.toString().decodeURL()])}'">Pause</button>
                        </g:if>
                        <g:else>
                            <button class="" ${SpringSecurityUtils.ifAllGranted(Roles.ROLE_JOB_ADMIN.toString()) ?: 'disabled'} onclick="document.location.href='${createLink(controller: 'job', action: 'activate', id: job.jobId, params: [origin: request.forwardURI.toString().decodeURL()])}'">Activate</button>
                        </g:else>

                        <button ${job.isActive() ?: 'disabled=true'} ${SpringSecurityUtils.ifAllGranted(Roles.ROLE_JOB_RUN.toString()) ?: 'disabled'} class="btn-text" onclick="document.location.href='${createLink(controller: 'job', action: 'run', id: job.jobId)}'">Run Job</button>

                        <button class="btn-text" ${SpringSecurityUtils.ifAllGranted(Roles.ROLE_JOB_ADMIN.toString()) ?: 'disabled'} onclick="if (confirm('Are you sure?')) {document.location.href='${createLink(controller: 'job', action: 'delete', id: job.jobId, params: [origin: request.forwardURI.toString().decodeURL()])}'}">Delete</button>
                    </div>
                </td>
            </tr>
        </g:each>
    </table>
</div>

</body>
</html>